//
//  ZWSearchView.swift
//  ZWWeibo
//
//  Created by flyiron on 16/4/16.
//  Copyright © 2016年 flyiron. All rights reserved.
//

import UIKit

@IBDesignable class ZWSearchView: UIView {
    
    private lazy var leftView:UIImageView = {
        var leftView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        leftView.contentMode = .Center
        return leftView
    }()
    @IBOutlet weak var rightCons: NSLayoutConstraint!
  
    @IBOutlet weak var searchField: UITextField!
    
    
    //搜索框开始编辑
    @IBAction func beginEidting(sender: AnyObject) {
        //通过约束来实现动画
        rightCons.constant = cancelBTN.frame.width
        UIView.animateWithDuration(0.25) { () -> Void in
            self.layoutIfNeeded()
        }
    }
    
    @IBOutlet weak var cancelBTN: UIButton!
    
    //点击取消按钮
    @IBAction func clickCancelBTN(sender: AnyObject) {
        rightCons.constant = 0
        UIView.animateWithDuration(0.25) { () -> Void in
            self.layoutIfNeeded()
        }
        searchField.resignFirstResponder()
    }
    
    //类方法从nib文件中加载view
    class func searchView() -> ZWSearchView {
        return NSBundle.mainBundle().loadNibNamed("ZWSearchView", owner: nil, options: nil).last! as! ZWSearchView
    }
    
   
    
    override func awakeFromNib() {
        leftView.frame.size = CGSizeMake(self.frame.height, self.frame.height)
        searchField.leftView = leftView
        searchField.leftViewMode = .Always
    }
}
